//
//  M4UIView.swift
//  M4
//
//  Created by YuChuang on 2024/8/8.
//

import Foundation
import UIKit


extension UIView {
    
    func M4AlertViewShow() {
        UIApplication.shared.keyWindow?.addSubview(self)
        self.transform = CGAffineTransform(scaleX: 1.1, y: 1.1)
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut) {
            self.alpha = 1
            self.transform = CGAffineTransform.identity
        }
    }

    func M4AlertViewHidden()  {
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseInOut) {
            self.alpha = 0
        } completion: { isCompleted in
            if isCompleted
            {
                self.removeFromSuperview()
            }
        }
    }
    
    func M4PopViewShow(subview: UIView) {
        UIApplication.shared.keyWindow?.addSubview(self)
        subview.frame = CGRect(x: 0, y: M4ScreenHeight, width: M4ScreenWidth, height: subview.frame.size.height)
        UIView.animate(withDuration: 0.3, delay: 0, options: .curveEaseOut) {
            subview.frame = CGRect(x: 0, y: M4ScreenHeight - subview.frame.size.height, width: M4ScreenWidth, height: subview.frame.size.height)
        } completion: { _ in

        }
    }

    func M4PopViewHidden(subview: UIView)  {
        UIView.animate(withDuration: 0.2, delay: 0, options: .curveEaseOut) {
            subview.frame = CGRect(x: 0, y: M4ScreenHeight, width: M4ScreenWidth, height: subview.frame.size.height);
        } completion: { _ in
            self.removeFromSuperview()
        }
    }
    
}

class M4TextView: UITextView {
    
    var placeHolder:String? {
        willSet{
            let placeHolderLabel = UILabel()
            placeHolderLabel.text = newValue
            placeHolderLabel.numberOfLines = 0
            placeHolderLabel.textColor = COLOR_999999
            placeHolderLabel.font = UIFont.systemFont(ofSize: 14, weight: .regular)
            addSubview(placeHolderLabel)
            setValue(placeHolderLabel, forKey: "_placeholderLabel")
        }
    }
}
